package com.danceforyou.medium.service.impl;

import com.danceforyou.common.restful.exception.BizExceptionEnum;
import com.danceforyou.common.service.impl.BaseServiceImpl;
import com.danceforyou.common.util.AssertUtil;
import com.danceforyou.common.util.Constants;
import com.danceforyou.medium.dao.MediumVoucherDAO;
import com.danceforyou.medium.model.MediumDO;
import com.danceforyou.medium.model.MediumVoucherDO;
import com.danceforyou.medium.service.MediumService;
import com.danceforyou.medium.service.MediumVoucherService;
import com.danceforyou.medium.vo.MediumVoucherVO;
import com.danceforyou.ossFile.model.OssFileDO;
import com.danceforyou.ossFile.service.OssFileService;
import com.danceforyou.user.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;

/**
* 类名称：MediumVoucherServiceImpl
* 创建人：danceforyou
* 创建时间：2021年02月01日
* 版本号：1.0v
*/
@Service("mediumVoucherService")
public class MediumVoucherServiceImpl extends BaseServiceImpl<Long, MediumVoucherDO, MediumVoucherVO, MediumVoucherDAO> implements MediumVoucherService {
    @Autowired
    private MediumVoucherDAO mediumVoucherDAO;
    @Autowired
    private MediumService mediumService;
    @Autowired
    private OssFileService ossFileService;
    @Autowired(required = false)
    private HttpSession session;

    @Override
    public MediumVoucherDAO getDao() {
        return mediumVoucherDAO;
    }

    @Override
    public void saveMediumVoucher(MultipartFile[] fileList, Long mediumId) {
        UserVO loginUser = (UserVO) session.getAttribute(Constants.SESSION_USER);
        AssertUtil.notNull(loginUser, BizExceptionEnum.AUTHFAILURE);
        MediumDO medium = mediumService.getById(mediumId);
        AssertUtil.notNull(medium, "公众号信息不存在");
        //删除原有的附件
        this.deleteByProperty("mediumId", medium.getId());
        //上传新的附件
        for (MultipartFile file : fileList) {
            OssFileDO ossFile = ossFileService.saveInfo(file);
            MediumVoucherDO voucher = new MediumVoucherDO();
            voucher.setMediumId(medium.getId());
            voucher.setOssFileId(ossFile.getId());
            this.insert(voucher);
        }
    }

    @Override
    public String listVoucherByMediumId(Long mediumId) {
        return mediumVoucherDAO.listVoucherByMediumId(mediumId);
    }
}
